package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssBucketOneEnum;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.knowledge.domain.SysPortalImages;
import cn.cdeden.knowledge.service.SysPortalImagesService;
import cn.cdeden.knowledge.service.dto.SysPortalImagesDto;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.RemoteFileService;
import cn.cdeden.resource.api.domain.*;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author Administrator
 * @date 2024/12/19 13:25
 * @describe:
 **/
@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "publicImages", version = "1.0.0")
public class OssUploadImageServiceImpl implements OssUploadService {
    private final SysPortalImagesService sysPortalImagesService;
    @DubboReference(check = false)
    private final RemoteFileService remoteFileService;
    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        SysPortalImages byId = sysPortalImagesService.getById(moduleId);
        if (byId == null) {
            throw new RuntimeException("图片不存在");
        }
        RemoteFile storage = remoteFileService.selectById(byId.getStorageId().toString());
        RemoteFile remoteFile = new RemoteFile();
        remoteFile.setUrl(storage.getUrl());
        remoteFile.setFileSize(storage.getFileSize());
        remoteFile.setName(storage.getName());
        remoteFile.setOriginalName(storage.getOriginalName());
        remoteFile.setOssId(storage.getOssId());
        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId) {
        JSONObject info1 = info.getInfo();
        if (info1 == null) {
            throw new RuntimeException("缺少必要参数info:{module:"+"模块名称"+"}");
        }else {
            Object module = info1.get("module");
            if (module == null) {
                throw new RuntimeException("缺少必要参数info:{module:" + "模块名称" + "}");
            }
            String string = module.toString();
            if (!(string.equals("enterprise") || string.equals("template") || string.equals("library"))) {
                throw new RuntimeException("不支持的模块类型，请新增模块类型");
            }
        }
        info.setBrandName(OssBucketOneEnum.PUBLIC_IMAGES.getCode());
        return info;
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        SysPortalImagesDto sysPortalImagesDto = new SysPortalImagesDto();
        sysPortalImagesDto.setModule(upload.getInfo().getString("module"));
        sysPortalImagesDto.setCreateTime(new Date());

        sysPortalImagesDto.setUrl(upload.getUrl());
        sysPortalImagesDto.setStorageId(Long.valueOf(upload.getStorageId()));
        sysPortalImagesDto.setName(upload.getFileName());
        sysPortalImagesService.insert(sysPortalImagesDto);
        sysPortalImagesDto.setStorageIdConvert(upload.getStorageId());
        RemoteFilesBucketPath remoteFilesBucketPath = RemoteFilesBucketPath.of(OssBucketOneEnum.PUBLIC_IMAGES.getCode());

        remoteFilesBucketPath.setInfo(JSONObject.parse(JSONObject.toJSONString(sysPortalImagesDto)));
        return remoteFilesBucketPath;
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        return List.of();
    }
}
